package com.chuangke.metadata.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.chuangke.common.db.page.CkPageQuery;
import com.chuangke.common.db.page.PageUtil;
import com.chuangke.common.exception.ChuangkeException;
import com.chuangke.framework.service.GridDictModelService;
import org.springframework.aop.framework.AopContext;
import org.springframework.stereotype.Service;

import com.chuangke.metadata.dao.SysBillDao;
import com.chuangke.metadata.entity.SysBill;
import com.chuangke.framework.service.impl.GridDictModelServiceImpl;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class SysBillService extends GridDictModelServiceImpl<SysBillDao, SysBill> {
    @Override
    public IPage<SysBill> page(CkPageQuery page) {
        List<SysBill> list = ((GridDictModelService) AopContext.currentProxy()).findList();
        String fuzzy = (String) page.get("name") ;
        if(!StrUtil.isBlank(fuzzy)) {
            list=list.stream().filter(item-> (item.getId().indexOf(fuzzy)!=-1||item.getName().indexOf(fuzzy)!=-1)).collect(Collectors.toList());
        }
        String moduleId=(String) page.get("moduleId") ;
        if(!StrUtil.isBlank(moduleId)) {
            list=list.stream().filter(item-> Objects.equals(item.getMoudleId(),moduleId)).collect(Collectors.toList());
        }
        return PageUtil.findPage(page, list);
    }

    public SysBill loadByBillType(String billType) {
        List<SysBill> list = ((GridDictModelService) AopContext.currentProxy()).findList();
        list=list.stream().filter(item->Objects.equals(item.getBillType(),billType)).collect(Collectors.toList());
        if(list==null||list.size()<1){
            throw new ChuangkeException("当前表单业务种类不存在，请检查");
        }
        return list.get(0);
    }
}
